package at.jku.se.fastsell.gui;

import at.jku.se.fastsell.FastsellUI;
import at.jku.se.fastsell.data.DataAccess;

import com.vaadin.server.VaadinSession;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.themes.Reindeer;

/**
 * Main UI component for login
 */
public class LoginComponent extends CustomComponent {

	private static final long serialVersionUID = 1L;

	private VerticalLayout mainLayout;
	private HorizontalLayout loginLayout;
	private Button btnRegister, btnLogin;
	private PasswordField passwordField;
	private TextField txtUsername;
	private Label lblHead, lblInfo;

	// ----------------------------------------------------------------------------------

	/**
	 * Creates a new UI LoginComponent
	 * 
	 * @param registerClick
	 *            - ClickListener for handling register button click
	 */
	public LoginComponent(ClickListener registerClick) {
		buildMainLayout(registerClick);
		setCompositionRoot(mainLayout);
	}

	// ----------------------------------------------------------------------------------

	private void buildMainLayout(ClickListener registerClick) {
		mainLayout = new VerticalLayout();
		mainLayout.setMargin(true);
		mainLayout.setSpacing(true);

		lblHead = new Label("FastSell Login");
		lblHead.setStyleName(Reindeer.LABEL_H1);
		mainLayout.addComponent(lblHead);

		loginLayout = new HorizontalLayout();
		loginLayout.setSpacing(true);
		mainLayout.addComponent(loginLayout);

		txtUsername = new TextField();
		txtUsername.setCaption("Username");
		txtUsername.setValue("user1");
		loginLayout.addComponent(txtUsername);

		passwordField = new PasswordField();
		passwordField.setCaption("Password");
		passwordField.setValue("pwd");
		loginLayout.addComponent(passwordField);

		btnLogin = new Button();
		btnLogin.setCaption("Login");
		btnLogin.addClickListener(new ClickListener() {

			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				try {

					String username = txtUsername.getValue();
					String pw = passwordField.getValue();

					// Some cheats ;)
					if (username.equals("del")) {
						DataAccess.clearDatabase();
						Notification.show("Cheat accpeted: Database cleared");
					} else if (username.equals("sampledata")) {
						DataAccess.fillWithSampleData();
						Notification.show("Cheat accpeted: Database cleared and filled with sample data");
					} else {
						// --
						if (DataAccess.verifyUser(username, pw)) {
							VaadinSession.getCurrent().getSession().setAttribute("user", username);
							FastsellUI.navigateTo(AllAuctionsView.NAME);
						} else {
							Notification.show("Username or password wrong, please try again!", Notification.Type.ERROR_MESSAGE);
						}
					}
				} catch (Exception e) {
					Notification.show("Login failed: " + e, Notification.Type.ERROR_MESSAGE);
				}
			}
		});
		loginLayout.addComponent(btnLogin);
		loginLayout.setComponentAlignment(btnLogin, Alignment.BOTTOM_CENTER);

		btnRegister = new Button();
		btnRegister.setCaption("Register");
		btnRegister.addClickListener(registerClick);
		loginLayout.addComponent(btnRegister);
		loginLayout.setComponentAlignment(btnRegister, Alignment.BOTTOM_CENTER);

		lblInfo = new Label("Service Engineering (259028/2014S) - Group 6");
		lblInfo.setStyleName(Reindeer.LABEL_SMALL);
		mainLayout.addComponent(lblInfo);
	}

}
